package controls;

import java.io.IOException;

import model.IModel;

import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;

/*
 * Rotates ("spins") spatial in given direction(s)
 */
public class RotationControl extends AbstractGameControl {

	private enum States {
		DEFAULT,
		AXIS;
	}
	
	private States state;
	
	private float xRate = 0.0f; //rad/s
	private float yRate = 0.0f;
	private float zRate = 0.0f;
	
	//optional axis to rotate around
	private Vector3f axis = new Vector3f();
	private float rate = 0.0f; //rad/s
	
	private Quaternion q;
	
	//public RotationControl(){}
	
	public RotationControl() {
		//spatial.addControl(this);
		this.q = new Quaternion();//spatial.getLocalRotation();
	}	
	
	public GameControl cloneForSpatial(Spatial spatial) {
		RotationControl other = new RotationControl();
		return other;
	}
	
	public void setRotationRates(float x, float y, float z) {
		this.xRate = x;
		this.yRate = y;
		this.zRate = z;
		this.state = States.DEFAULT;
	}
	
	public void setRotationAxis(Vector3f axis, float rate) {
		this.axis = axis.normalize();
		this.rate = rate;
		this.state = States.AXIS;		
	}

	@Override
	protected void controlUpdate(float tpf) {
		if (this.state.equals(States.DEFAULT)) {
			q.fromAngles(xRate*tpf, yRate*tpf, zRate*tpf);
			this.spatial.rotate(q);
		}
		else if (this.state.equals(States.AXIS)) {
			q.fromAngleNormalAxis(rate*tpf, axis);
			this.spatial.rotate(q);
		}		
	}

	@Override
	protected void controlRender(RenderManager rm, ViewPort vp) {
				
		
	}

	
}
